

# 临时的相机管理 为完成D5a 的头图拍摄

from D5Core.Details.D5Api import D5Api
from D5Core.D5Common import D5Singleton,Vector3
import json
from D5Core.Details.D5Api import APIRequestType


@D5Singleton
class CameraTool:

    def __init__(self):
        pass

    def get_camera_location(self):
        req_id = D5Api().generate_uuid()
        payload = {
            "uuid": req_id,
            "type": APIRequestType.CAMERA_INFO.value,
            "content": "{}"
        }
        res = D5Api().request_sync(req_id, json.dumps(payload))
        res = json.loads(res)
        success = res.get("success", False)
        if success:
            res = res.get("response", "")
            res = json.loads(res)
            return  Vector3.from_str(res.get("location", "0,0,0"))
        return Vector3(0, 0, 0)
    def set_camera_location(self, location: Vector3):
        req_id = D5Api().generate_uuid()
        payload = {
            "uuid": req_id,
            "type": APIRequestType.UPDATE_CAMERA_INFO.value,
            "content": json.dumps({"location": location.to_str()})
        }
        res = D5Api().request_sync(req_id, json.dumps(payload))
        res = json.loads(res)
        return res.get("success", False)
    
    def get_camera_rotation(self):
        req_id = D5Api().generate_uuid()
        payload = {
            "uuid": req_id,
            "type": APIRequestType.CAMERA_INFO.value,
            "content": "{}"
        }
        res = D5Api().request_sync(req_id, json.dumps(payload))
        res = json.loads(res)
        success = res.get("success", False)
        if success:
            res = res.get("response", "")
            res = json.loads(res)
            return Vector3.from_str(res.get("rotation", "0,0,0"))
        return Vector3(0, 0, 0)
    def set_camera_rotation(self, rotation: Vector3):
        req_id = D5Api().generate_uuid()
        payload = {
            "uuid": req_id,
            "type": APIRequestType.UPDATE_CAMERA_INFO.value,
            "content": json.dumps({"rotation": rotation.to_str()})
        }
        res = D5Api().request_sync(req_id, json.dumps(payload))
        res = json.loads(res)
        return res.get("success", False)
    def get_camera_fov(self):
        req_id = D5Api().generate_uuid()
        payload = {
            "uuid": req_id,
            "type": APIRequestType.CAMERA_INFO.value,
            "content": "{}"
        }
        res = D5Api().request_sync(req_id, json.dumps(payload))
        res = json.loads(res)
        success = res.get("success", False)
        if success:
            res = res.get("response", "")
            res = json.loads(res)
            return res.get("fov", 0.0)
        return 0.0
    def set_camera_fov(self, fov: float):
        req_id = D5Api().generate_uuid()
        payload = {
            "uuid": req_id,
            "type": APIRequestType.UPDATE_CAMERA_INFO.value,
            "content": json.dumps({"fov": fov})
        }
        res = D5Api().request_sync(req_id, json.dumps(payload))
        res = json.loads(res)
        return res.get("success", False)